package com.csd.action.team;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.csd.helper.ActionResult;
import com.csd.helper.Tool;
import com.csd.pojo.ProjectType;
import com.csd.pojo.Student;
import com.csd.pojo.Team;
import com.csd.serviceImpl.ProjectTypeService;
import com.csd.serviceImpl.StudentService;
import com.csd.serviceImpl.TeamService;
/**
 * 
 * Action：添加新成员
 * @author csd
 * @version 1.0
 * 
 * <br> 修订时间：2009-12-17
 * <br> 修改原因：创建文件
 * <br> 修改人：刘世彬
 * 
 * <br> 
 * 
 */
public class MemberAddAction {
	/**新成员*/
	private Student member;
	
	private StudentService service;
	
	private ProjectTypeService projectTypeService;
	private TeamService teamService;
	
	private ProjectType projectType;
	
	private Integer projectTypeID;
	private Integer teamID;
	private Team team;
	private List<Student> members;
	
	public List<Student> getMembers() {
		return members;
	}


	public void setMembers(List<Student> members) {
		this.members = members;
	}


	public Team getTeam() {
		return team;
	}


	public void setTeam(Team team) {
		this.team = team;
	}


	public Integer getTeamID() {
		return teamID;
	}


	public void setTeamID(Integer teamID) {
		this.teamID = teamID;
	}


	public ProjectTypeService getProjectTypeService() {
		return projectTypeService;
	}


	public void setProjectTypeService(ProjectTypeService projectTypeService) {
		this.projectTypeService = projectTypeService;
	}


	public TeamService getTeamService() {
		return teamService;
	}


	public void setTeamService(TeamService teamService) {
		this.teamService = teamService;
	}


	public Integer getProjectTypeID() {
		return projectTypeID;
	}


	public void setProjectTypeID(Integer projectTypeID) {
		this.projectTypeID = projectTypeID;
	}


	public StudentService getService() {
		return service;
	}


	public void setService(StudentService service) {
		this.service = service;
	}


	public Student getMember() {
		return member;
	}


	public void setMember(Student member) {
		this.member = member;
	}

	public ProjectType getProjectType() {
		return projectType;
	}


	public void setProjectType(ProjectType projectType) {
		this.projectType = projectType;
	}

	public String execute()
	{
		String result = ActionResult.fail;
		int teamMemberNum;//获取团队成员人数
	    teamMemberNum = teamService.getTeamMemberNumberByID(this.teamID);
	    int max;//项目类型人数上限
		max = projectTypeService.getProjectTypeMaxMemberByID(projectTypeID);
	    if(teamMemberNum < max)
	    {
	    	if(service.addMember(this.member)){
	    		result = ActionResult.success;
	    		}
		}
	    else
	    {
	    	result = ActionResult.fail;
	    }
	    
	    /*以下部分是从teamShow.action拷贝过来，主要是为了解决跳转问题
	          方法不太好，以后有时间要改进跳转结构*/
        this.team = teamService.getTeam();
		HttpSession session = ServletActionContext.getRequest().getSession();
		// 存储团队是否已经申请项目的状态
		boolean flag = false;
			
		if(this.team != null)
		{
			this.members = Tool.setToList(this.team.getMembers());
			this.teamID = this.team.getId();
			flag = teamService.hasProjectOrNot(this.team.getId());
			result = ActionResult.success;
		}	
		// 判断跳转链接是否存在projectTypeID属性
		try 
		{
			int type = this.projectTypeID;
			if (type > 0)
				flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		session.setAttribute("hasProjectOrNot", flag);	
	    
	    
		return result;
	}	
}
